package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.BaseResponse;
import org.example.common.ResultUtils;
import org.example.constant.UserConstant;
import org.example.domain.Feedback;
import org.example.domain.User;

import org.example.service.FeedBackService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/feedback")
public class FeedBackController {
    @Resource
    private FeedBackService feedBackService;
    @GetMapping("/get")
    public BaseResponse<String> getFeedback(HttpServletRequest request) {
         User user = (User)request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        System.out.println("getFeedback"+request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE));
        int userId = user.getId();
        Feedback feedback = feedBackService.getOne(new QueryWrapper<Feedback>().eq("user_id", userId));
        return ResultUtils.success(feedback.getContent());
    }
    @PostMapping("/add")
    public BaseResponse<Boolean> addFeedback(@RequestParam("content")String  content, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        int userId = user.getId();
        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        feedback.setContent(content);
        boolean result = feedBackService.save(feedback);
        if (!result) {
            throw new RuntimeException("添加失败");
        }
        return ResultUtils.success(null);
    }
}
